<!--通用普通输入框组件-->
<template>
  <a-form-item :label="label">
    <a-input
        :style="getStyle"
        :placeholder="!isDisabled?cplaceholder:''"
        :disabled="isDisabled"
        v-decorator="vDecorator"
        @click="onClick"
        @change="change"
        autocomplete="off"
        @blur="onBlur"
        :readOnly="creadOnly"
        :maxLength="maxLength"
        :class="{'c-form-disabled':isDisabled,'c-form-input':true}"
    >
      <template slot="suffix">
        <slot name="suffix"></slot>
      </template>
    </a-input>
  </a-form-item>
</template>

<script>
import './css/form.less'
import { mixin } from './js/form.js'

export default {
  name: 'AntFormInput',
  mixins: [mixin],
  props: {
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 是否由form表单自动布局
     */
    autoLayoutByForm: {
      type: Boolean,
      default: true
    },
    /**
     * 显示文本
     */
    label: {
      type: String,
      default: ''
    },
    /**
     * 是否只读
     */
    readOnly: {
      type: Boolean,
      default: false
    },
    /**
     * 输入提示
     */
    placeholder: {
      type: String,
      default: ''
    },
    /**
     * 绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     */
    vDecorator: {
      type: Array,
      default: () => []
    },
    /**
     * 最大输入字符限制
     */
    maxLength: {
      type: Number,
      default: 200
    },
    /**
     * css样式，优先级最高
     */
    cstyle: {
      type: String,
      default: ''
    },
    /**
     * 非自动布局的宽度，优先级最低
     */
    defaultWidth: {
      type: Number,
      default: 240
    }
  },
  data() {
    return {
      //是否禁用和输入提示，由AntForm组件调用
      cdisabled: false,
      cplaceholder: this.placeholder ? this.placeholder : '请输入' + this.label,
      creadOnly: this.readOnly
    }
  },
  computed: {
    /**
     * 获取样式
     */
    getStyle() {
      return { width: (this.percentageLayoutWidth ? this.percentageLayoutWidth : this.defaultWidth) + 'px', ...this.cstyle }
    }
  },
  created() {
  },
  mounted() {
    // 自动添加验证为空时的message
    let vDecorator = this.vDecorator
    if (vDecorator.length > 1 && vDecorator[1].rules) {
      let rules = vDecorator[1].rules
      rules.forEach(obj => {
        if (obj.required && !obj.message) {
          obj.message = '请选择' + this.label
        }
      })
    }
  },
  beforeDestroy() {
  },
  methods: {
    /**
     * 点击事件
     * @param val
     */
    onClick(val) {
      this.$nextTick(() => {
        this.$emit('click', val)
      })
    },
    /**
     * 输入内容改变事件
     * @param value
     * @param data
     */
    change(value, data) {
      this.$nextTick(() => {
        this.$emit('change', value, data)
      })
    },
    /**
     * 失去焦点事件
     * @param event
     */
    onBlur(event) {
      this.$emit('blur', event.target.value)
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .ant-input:hover,
::v-deep .ant-input:focus {
  border-color: @primary-color;
}
</style>
